import { create } from 'zustand';
import {
	addCourseChapterApi,
	addCourseVideoApi,
	courseAddApi,
	courseListApi,
	courseUpdateApi,
	deleteCourseListApi,
	getCourseChapterApi,
	getCourseChapterInfoApi,
	getCourseInfoApi,
	getcourseItemApi,
	updateCourseChapterApi,
} from '../apis/courseApi';
import type {
	chapterListInterface,
	chapterParamsInterface,
	courseAddObject,
	courseChapterInfo,
	courseItemsInterface,
	courseOutlineObject,
	courseParamsInterface,
	lessonParamsInterface,
	ResPublishCourseInfo,
} from '../models/Course';

interface courseState {
	bears: number;
	increase: (by: number) => void;
	// 课程添加
	courseId: string;
	// 修改课程id
	setCourseId: (id: string) => void;
	current: number;
	setCurrent: (by: number) => void;
	courseAdd: (courseId: courseAddObject) => Promise<void>;
	// 修改课程
	courseUpdate: (courseId: courseAddObject) => Promise<void>;
	// 创建课程大纲
	// 课程信息的回显
	courseInfo: courseOutlineObject;
	// 根据课程id查询课程基本信息
	getCourseInfo: (courseId: string) => Promise<void>;
	// 修改课程id查询的课程信息
	setCourseInfo: (courseInfo: courseOutlineObject) => void;
	// 根据课程id确定课程大纲
	getcourseItem: (id: string) => Promise<void>;
	// 课程信息的确定回显
	courseOutLine: ResPublishCourseInfo;
	setCourseOutLine: (courseOutLine: ResPublishCourseInfo) => void;

	// 添加章节
	addCourseChapter: (data: chapterParamsInterface) => Promise<void>;
	// 编辑章节
	updateCourseChapter: (data: chapterParamsInterface) => Promise<void>;
	// 添加小节
	addCourseSection: (data: lessonParamsInterface) => Promise<void>;

	// 章节列表
	chapterList: chapterListInterface[];
	// 获取单个章节信息
	getCourseChapterInfo: (chapterId: string) => Promise<courseChapterInfo>;
	// 修改章节列表
	setChapterList: (chapterList: chapterListInterface[]) => void;
	// 根据课程id查询课程大纲列表
	getChapterList: (courseId: string) => Promise<void>;
	// 课程列表
	courseList: courseItemsInterface[];
	getCourseList: (params: courseParamsInterface) => Promise<void>;
	params: courseParamsInterface;
	total: number;
	deleteCourseList: (courseId: string) => Promise<void>;
}

export const useCourseStore = create<courseState>()((set) => ({
	bears: 0,
	increase: (by) => set((state) => ({ bears: state.bears + by })),
	// 课程添加
	courseId: '',
	setCourseId: (id) => set(() => ({ courseId: id })),
	current: 0,
	setCurrent: (by) => set(() => ({ current: by })),
	courseAdd: async (data) => {
		const res = await courseAddApi(data);
		set({ courseId: res.data.courseId });
	},
	// 课程修改
	courseUpdate: async (data) => {
		await courseUpdateApi(data);
	},

	// 课程基本信息回显
	courseOutLine: {},
	setCourseOutLine: (courseOutLine) => set(() => ({ courseOutLine: courseOutLine })),
	getcourseItem: async (courseId) => {
		const res = await getcourseItemApi(courseId);
		set({ courseOutLine: res.data });
	},

	// 章节列表
	chapterList: [],
	// 修改章节列表
	setChapterList: (chapterList) => set(() => ({ chapterList: chapterList })),
	// 课程信息的回显
	courseInfo: {},
	// 根据章节id查询章节信息
	getCourseInfo: async (courseId) => {
		const res = await getCourseInfoApi(courseId);
		set({ courseInfo: res.data });
	},
	// 修改课程id查询的课程信息
	setCourseInfo: (courseInfo) => set(() => ({ courseInfo: courseInfo })),

	// 根据课程id确定课程大纲列表
	getChapterList: async (courseId) => {
		const res = await getCourseChapterApi(courseId);
		// console.log(res);
		set({ chapterList: res.data.allChapterVideo });
	},
	// 获取单个章节信息
	getCourseChapterInfo: async (chapterId) => {
		const res = await getCourseChapterInfoApi(chapterId);
		return res.data.chapter;
	},

	// 添加章节
	addCourseChapter: async (data) => {
		await addCourseChapterApi(data);
	},
	// 编辑章节
	updateCourseChapter: async (data) => {
		await updateCourseChapterApi(data);
	},
	// 添加小节
	addCourseSection: async (data) => {
		await addCourseVideoApi(data);
	},

	// 课程列表
	params: { pageNum: 1, pageSize: 5 },
	courseList: [],
	total: 0,
	getCourseList: async (params) => {
		const res = await courseListApi(params);
		set({ courseList: res.data.data });
		set({ total: res.data.total });
	},
	deleteCourseList: async (id: string) => {
		await deleteCourseListApi(id);
	},
}));
